//
//  ClinTrialWebViewController.m
//  ClinicalCollections
//
//  Created by janakiraman gopinath on 12/31/12.
//  Copyright (c) 2012 GOPI. All rights reserved.
//

#import "ClinTrialWebViewController.h"
#import "CommonAPI.h"

@interface ClinTrialWebViewController () <UIWebViewDelegate, UIAlertViewDelegate>
@property (nonatomic, strong) UIActivityIndicatorView *spinner;
@end

@implementation ClinTrialWebViewController

@synthesize webView = _webView;
@synthesize url = _url;
@synthesize spinner = _spinner;
@synthesize ccvsCustomData = _ccvsCustomData;


- (UIActivityIndicatorView *)spinner {
    if (!_spinner) {
        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
            _spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
            _spinner.center = CGPointMake(self.view.frame.size.width/2, self.view.frame.size.height/2);
            _spinner.frame = CGRectMake(_spinner.frame.origin.x - 40, _spinner.frame.origin.y - 40, 80, 80);
            _spinner.backgroundColor = [UIColor whiteColor];
            _spinner.color = [UIColor redColor];
            [self.view addSubview:_spinner];
        } else {
            _spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
        }
    }
    return _spinner;
}

-(void)setCcvsCustomData:(CCVSString *)ccvsCustomData
{
    _ccvsCustomData = ccvsCustomData;
}


- (void) setUrl:(NSString *)url
{
    _url = url;
    [self loadPage:url];
    
}
- (void) loadPage:(NSString *) url
{
    
    if (UI_USER_INTERFACE_IDIOM() != UIUserInterfaceIdiomPad) {
        self.navigationItem.titleView = self.spinner;
    }
    [self.webView addSubview:self.spinner];
    [self.spinner startAnimating];

    [[CommonAPI class] download:^{
        [[CommonAPI class] main:^{
            self.webView.scalesPageToFit = YES;
            [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]];
            
        }];
    }];
    
}

- (void)viewWillAppear:(BOOL)animated
{
	[super viewWillAppear:animated];
    [self.webView setDelegate:self];
    if (_ccvsCustomData) {
        if (!self.navigationController) {
            UIToolbar *toolBar;
            toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 50)];
            //toolBar.frame = CGRectMake(0, 0, 320, 50);
            toolBar.barStyle = UIBarStyleDefault;
            //[toolBar sizeToFit];
            UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
            UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneAction:)];
            
            
            NSArray *barButton  =   [[NSArray alloc] initWithObjects:flexibleSpace,doneButton,nil];
            [toolBar setItems:barButton];
            [self.view addSubview:toolBar];
            
        }
        NSString * html = [NSString stringWithFormat:@"<html><body  text=\"#000000\"><br><br><br><br><br><pre>  %@</pre></body></html><head><style>* {margin:0;padding:0; font-family:Arial;font-size:20px;text-align:justify}</style></head>", self.ccvsCustomData.string];
        self.webView.scalesPageToFit = YES;
        [self.webView loadHTMLString:html baseURL:nil];
        
    }
    
}

-(IBAction)doneAction:(UIBarButtonItem*)sender
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void) viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return YES;
}

#pragma mark -- WebView Delegate 

- (void) webViewDidFinishLoad:(UIWebView *)webView
{
    [self.spinner stopAnimating];
    [self.spinner removeFromSuperview];
}


- (void) webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    [self.webView stopLoading];
    NSString *errorMsg = [NSString stringWithFormat:@"%@" , error];
    [self setupAlertView:errorMsg];
    
}
- (void) setupAlertView:(NSString *) errorMsg 
{
    UIAlertView *alertView= [[UIAlertView alloc] initWithTitle:@"Error" message:errorMsg delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alertView show];
    [self.spinner stopAnimating];
}

- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    [alertView dismissWithClickedButtonIndex:buttonIndex animated:YES];
}
- (void) webViewDidStartLoad:(UIWebView *)webView
{
     
}

@end
